
//------------------------类-------------------------------
class Greeter{
    greeting: string;
    constructor(message: string){
        this.greeting = message;
    }
    greet(){
        return "Hello, "+this.greeting;
    }
}
let greeter = new Greeter("World");
document.write(`<p>${greeter.greet()}</p>`);

class CAnimal{
    name: string;
    constructor(theName: string){
        this.name = theName;
    }
    // 编译成js的原型方法
    move(distanceMeters: number = 0) {
        console.log(`${this.name} moved ${distanceMeters}m.`);
    }
    // 编译成js的对象方法
    move2 = (distanceMeters: number = 0)=>{
        console.log(`${this.name} moved ${distanceMeters}m.`);
    }
    static move3 = (distanceMeters: number = 0)=>{
        console.log("static move3");
    }
    static move4(distanceMeters: number = 0) {
        console.log("static move4");
    }
}

class CSnake extends CAnimal{
    constructor(name: string){super(name);}
    //constructor(){super("ljf");}  // 只能有一个构造函数吗
    move(distanceMeters = 5){
        console.log("Slithering...");
        super.move(distanceMeters);
    }
}
class CHouse extends CAnimal{
    constructor(name: string){super(name);}
    move(distanceMeters = 45){
        console.log("Galloping...");
        super.move(distanceMeters);
    }
}
let sam = new CSnake("Samy the Python");
let tom: CAnimal = new CHouse("Tomy the Palomino");

sam.move();
tom.move(34);

// 公共，私有与受保护的修饰符
// 默认为公有

// 理解私有，结构性类型系统，有private成员时就不一样了，必须还是同一来源
class Animal2{
    private name: string;
    constructor(theName: string){
        this.name = theName;
    }
}
class Rhino extends Animal2{
    constructor(){
        super("Rhino");
    }
}
class Employee{
    private name: string;
    constructor(theName: string){
        this.name = theName;
    }
}
let animal = new Animal2("Goat");
let rhino = new Rhino();
let employee = new Employee("Bob");

animal = rhino;
rhino = animal;     // 反过来也可以，如果Rhino中多一个成员就不行了
//animal = employee;  // private成员不是同一个来源

//------------------静态成员----------------------
class Grid{
    static origin = {x:0, y:0};
    calculateDis(point:{x:number;y:number}){
        let xDist = (point.x-Grid.origin.x);
        let yDist = (point.y-Grid.origin.y);
        return Math.sqrt(xDist*xDist+yDist*yDist)/this.scale;
    }
    constructor(public scale: number){
    }
}
let grid1 = new Grid(1.0);
let grid2 = new Grid(4.0);
document.write(`<p>${grid1.calculateDis({x:10, y:10})}`);
document.write(`<p>${grid2.calculateDis({x:10, y:10})}`);

//--------------------抽象类------------------------
abstract class AbsAnimal{
    abstract makeSound():void;
    move():void{
        console.log("roaming the earch...");
    }
}

//-------------------this 和 箭头函数--------------------
// JavaScript里，this的值在函数被调用的时候才会指定
let deck = {
    suits: ["hearts","spades","clubs","diamonds"],
    cards: Array(52),
    createCardPickeer: function(){
        document.write("<p>createCardPicker</p>")
        return () => { // 添加箭头函数编译后js文件添加：var _this = this;
            document.write("<p>return function</p>")
            let pickedCard=Math.floor(Math.random()*52);
            let pickedSuit=Math.floor(pickedCard/13);
            return{suit:this.suits[pickedSuit],card:pickedCard%13};
        }
    }
}
document.write("<p>before deck.createCardPicker</p>")
let cardPicker = deck.createCardPickeer(); // 这是调用函数
document.write("<p>end deck.createCardPicker</p>")
let pickedCard = cardPicker();  // 调用返回的函数
document.write("<p>end cardPicker</p>")
document.write("<p>card: "+pickedCard.card+" of "+pickedCard.suit+"</p>");

//------------------this参数在回调函数里调用-------------------
interface UIElement{
    addClickListener(onClick:(this:void,e:Event)=>void):void;
}
class Handler{
    info: string;
    onClickBad(this: Handler, e: Event){
        //this.info = e.message;
    };
}
let h = new Handler();
//uiElement.addClickListener(h.onClickBad); // error!
console.log("dlfjf");




















